import Vue from "vue";
import VueRouter from "vue-router";
/* Layout */
import Layout from '@/layout'

Vue.use(VueRouter);

// 重写push replace
// 先把VueRouter原型对象的push保存一份
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace
// 第一个参数：告诉原来的push方法，你往哪里跳转（传递那些参数）
// 第二个参数是成功的回调
// 第三个参数是失败的回调
VueRouter.prototype.push = function(location, resolve, reject) {
  // 如果路由跳转的时候又传递resolve和reject
  if (resolve && reject) {
    // 不能直接写originPush()，因为它是指向windows，所以需要用call来改变this指向，并且调用一次
    originPush.call(this, location, resolve, reject)
  } else {
    // 如果没有传递，就手动传递两个回调
    originPush.call(this, location, () => {}, () => {})
  }
}

VueRouter.prototype.replace = function(location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject)
  } else {
    originReplace.call(this, location, () => {}, () => {})
  }
}

const routes = [
  {
    path:'/layout',
    name:'layout',
    component:()=>import('@/layout/index.vue')
  },
  {
    path: "/",
    name: "index",
    component:Layout,
    children:[{
      path:'',
      component: () => import("@/views/index/index.vue")
    }]
  },
  // 房产经纪
  {
    path:'/agents',
    name:'agents',
    component: Layout,
      children:[{
        path:'',
        component:() => import('@/views/agents/index.vue')
      }]
    
  },
  // 地产分类
  {
    path:'/category',
    name:'category',
    component:Layout,
    children:[{
      path:'',
      component:() => import('@/views/category/index.vue')
    }]
  },
  // 地产列表
  {
    path:'/property/:id',
    name:'property',
    component:Layout,
    children:[{
      path:'',
      component:()=>import('@/views/property/index.vue')
    }]
  },
  // 地产详情
  {
    path:'/detail/:id',
    name:'detail',
    component:Layout,
    children:[{
      path:'',
      component:()=>import('@/views/detail/index.vue')
    }]
  },
  // 关于我们
  {
    path:'/page',
    name:'page',
    component:Layout,
    children:[{
      path:'',
      component:()=>import('@/views/page/index.vue')
    }]
  }
]

const router = new VueRouter({ routes });

export default router;